package com.lhb.components;

import com.lhb.common.components.LeftPanelButton;
import com.lhb.common.components.TopPanelButton;
import com.lhb.common.components.TopPanelJTextField;
import com.lhb.common.constant.common;
import com.lhb.common.utils.ExcelUtils;
import com.lhb.common.utils.ListUtils;
import com.lhb.pojo.entity.Award;
import com.lhb.pojo.entity.Family;
import com.lhb.pojo.entity.Student;
import com.lhb.pojo.vo.AwardVo;
import com.lhb.pojo.vo.CourseVo;
import com.lhb.pojo.vo.FamilyVo;
import com.lhb.service.MainJFrameService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Component
public class MainJFrame extends JFrame {

    private JPanel leftPanel, centerPanel,topPanel;
    private JButton jButton1,jButton2,jButton3,jButton4,jButton5,jButton6,jButton7;
    private DefaultTableModel defaultTableModel;
    private JLabel jLabel,jLabel1,jLabel2,jLabel3;
    private JComboBox<String> jComboBox,jComboBox1;
    private TopPanelJTextField topPanelJTextField,topPanelJTextField1;
    private TopPanelButton topPanelButton,topPanelButton1,topPanelButton2,topPanelButton3,topPanelButton4;
    private JTable jTable;
    @Autowired
    private MainJFrameService mainJFrameService;

    public MainJFrame createMainJFrame()
    {
        MainJFrame mainJFrame = new MainJFrame();
        mainJFrame.setIconImage(new ImageIcon("src/main/resources/image/mainLogo.png").getImage());
        mainJFrame.setTitle("学籍管理");

        // 将JFrame的状态设置为最大化
        mainJFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        mainJFrame.setLocationRelativeTo(null);
        mainJFrame.setLayout(null);
        mainJFrame.setResizable(false);
        mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        leftPanel = new JPanel();
        leftPanel.setBounds(0, 0, 200, 840);
        leftPanel.setBackground(Color.DARK_GRAY);
        leftPanel.setLayout(null);

        topPanel = createTopPanelAndCenterPanel();

         topPanelButton4 = new TopPanelButton("导出数据",new Color(175, 157, 29),new Color(216, 181, 12));

        topPanel.add(topPanelButton4);

        topPanelButton4.setBounds(1235,33,100,30);

        topPanelButton4.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                ExcelUtils.exportToExcel(jTable);
            }
        });

        jButton1 = new LeftPanelButton("学生信息管理");
        jButton1.setBounds(10,10,180,50);
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                topPanel.remove(topPanelButton4);

                topPanelButton4 = new TopPanelButton("导出数据",new Color(175, 157, 29),new Color(216, 181, 12));

                topPanel.add(topPanelButton4);

                topPanelButton4.setBounds(1235,33,100,30);

                topPanelButton4.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {
                        ExcelUtils.exportToExcel(jTable);
                    }
                });


                topPanel.remove(topPanelButton);
                topPanelButton = new TopPanelButton("查询", Color.BLACK, Color.DARK_GRAY);

                topPanelButton.setBounds(850,33,70,30);

                topPanel.add(topPanelButton);




                topPanel.remove(topPanelButton3);

                topPanelButton3 = new TopPanelButton("保存", new Color(253, 184, 0), new Color(198, 151, 26));

                topPanelButton3.setBounds(1150,33,70,30);

                topPanel.add(topPanelButton3);

                String a[]={"年级","专业","班级","学号","姓名","性别","年龄","出生日期","国籍","家庭地址","是否优待家庭","个人电话号码"};
                Student student = new Student();
                List<Student> students = mainJFrameService.getStudents(student);


                String[][] d = ListUtils.ListToTwoArrayStudent(students, a.length);


                defaultTableModel.setDataVector(d,a);



                jTable.setModel(defaultTableModel);

                topPanelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        Student student = new Student();
                        String grade,profession,myClass,studentId;

                        grade=jComboBox.getSelectedItem().toString();
                        profession=topPanelJTextField.getText();
                        myClass=jComboBox1.getSelectedItem().toString();
                        studentId=topPanelJTextField1.getText();

                        student.setGrade(grade);
                        student.setProfession(profession);
                        student.setMyClass(myClass);
                        student.setStudentId(studentId);


                        List<Student> students = mainJFrameService.getStudents(student);


                        String[][] d = ListUtils.ListToTwoArrayStudent(students, a.length);


                        defaultTableModel.setDataVector(d,a);



                        jTable.setModel(defaultTableModel);


                    }
                });




                topPanelButton3.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {

                        Vector<Vector> dataVector = defaultTableModel.getDataVector();



                            mainJFrameService.saveStudent(dataVector);


                    }
                });

            }
        });
        jButton1.setSelected(true);

        leftPanel.add(jButton1);

        jButton2 = new LeftPanelButton("学生家庭信息管理");
        jButton2.setBounds(10,70,180,50);
        jButton2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                topPanel.remove(topPanelButton4);

                topPanel.remove(topPanelButton);
                topPanelButton = new TopPanelButton("查询", Color.BLACK, Color.DARK_GRAY);

                topPanelButton.setBounds(850,33,70,30);

                topPanelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        Student student = new Student();
                        String grade,profession,myClass,studentId;

                        grade=jComboBox.getSelectedItem().toString();
                        profession=topPanelJTextField.getText();
                        myClass=jComboBox1.getSelectedItem().toString();
                        studentId=topPanelJTextField1.getText();

                        student.setGrade(grade);
                        student.setProfession(profession);
                        student.setMyClass(myClass);
                        student.setStudentId(studentId);


                        List<Student> students = mainJFrameService.getStudents(student);

                        List<FamilyVo> list = mainJFrameService.getFamilies(students) ;
                        String a[]={"年级","专业","班级","学号","姓名","性别","称谓","家长姓名","工作地点","职业","个人电话号码"};
                        String[][] d = ListUtils.ListToTwoArrayFamily(list,a.length);


                        defaultTableModel.setDataVector(d,a);



                        jTable.setModel(defaultTableModel);


                    }
                });

                topPanel.add(topPanelButton);

                topPanel.remove(topPanelButton3);

                topPanelButton3 = new TopPanelButton("保存", new Color(253, 184, 0), new Color(198, 151, 26));

                topPanelButton3.setBounds(1150,33,70,30);

                topPanelButton3.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {

                        Vector<Vector> dataVector = defaultTableModel.getDataVector();




                            mainJFrameService.saveFamily(dataVector);



                    }
                });


                topPanel.add(topPanelButton3);




                String a[]={"年级","专业","班级","学号","姓名","性别","称谓","家长姓名","工作地点","职业","个人电话号码"};
                Student student = new Student();
                List<Student> students = mainJFrameService.getStudents(student);
                List<FamilyVo> familyVoList = mainJFrameService.getFamilies(students);

                String[][] strings = ListUtils.ListToTwoArrayFamily(familyVoList, a.length);

                defaultTableModel.setDataVector(strings,a);
                jTable.setModel(defaultTableModel);

            }
        });
        leftPanel.add(jButton2);

        jButton3 = new LeftPanelButton("学生获奖信息管理");
        jButton3.setBounds(10,130,180,50);
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                topPanel.remove(topPanelButton4);

                topPanel.remove(topPanelButton);
                topPanelButton = new TopPanelButton("查询", Color.BLACK, Color.DARK_GRAY);
                topPanel.add(topPanelButton);
                topPanelButton.setBounds(850,33,70,30);


                topPanelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        Student student = new Student();
                        String grade,profession,myClass,studentId;

                        grade=jComboBox.getSelectedItem().toString();
                        profession=topPanelJTextField.getText();
                        myClass=jComboBox1.getSelectedItem().toString();
                        studentId=topPanelJTextField1.getText();

                        student.setGrade(grade);
                        student.setProfession(profession);
                        student.setMyClass(myClass);
                        student.setStudentId(studentId);


                        List<Student> students = mainJFrameService.getStudents(student);

                        List<AwardVo> list = mainJFrameService.getAward(students) ;
                        String a[]={"年级","专业","班级","学号","姓名","性别","称谓","家长姓名","工作地点","职业","个人电话号码"};
                        String[][] d = ListUtils.ListToTwoArrayAward(list,a.length);


                        defaultTableModel.setDataVector(d,a);



                        jTable.setModel(defaultTableModel);


                    }
                });
                topPanel.remove(topPanelButton3);

                topPanelButton3 = new TopPanelButton("保存", new Color(253, 184, 0), new Color(198, 151, 26));

                topPanelButton3.setBounds(1150,33,70,30);

                topPanel.add(topPanelButton3);

                topPanelButton3.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {

                        Vector<Vector> dataVector = defaultTableModel.getDataVector();




                        mainJFrameService.saveAward(dataVector);



                    }
                });


                List<Student> students = mainJFrameService.getStudents(new Student());

                List<AwardVo> list = mainJFrameService.getAward(students) ;
                String a[]={"年级","专业","班级","学号","姓名","性别","称谓","家长姓名","工作地点","职业","个人电话号码"};
                String[][] d = ListUtils.ListToTwoArrayAward(list,a.length);


                defaultTableModel.setDataVector(d,a);



                jTable.setModel(defaultTableModel);



            }
        });
        leftPanel.add(jButton3);

        jButton4 = new LeftPanelButton("学生课程信息管理");
        jButton4.setBounds(10,190,180,50);
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                topPanel.remove(topPanelButton4);

                topPanel.remove(topPanelButton);
                topPanelButton = new TopPanelButton("查询", Color.BLACK, Color.DARK_GRAY);
                topPanel.add(topPanelButton);
                topPanelButton.setBounds(850,33,70,30);


                topPanelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        Student student = new Student();
                        String grade,profession,myClass,studentId;

                        grade=jComboBox.getSelectedItem().toString();
                        profession=topPanelJTextField.getText();
                        myClass=jComboBox1.getSelectedItem().toString();
                        studentId=topPanelJTextField1.getText();

                        student.setGrade(grade);
                        student.setProfession(profession);
                        student.setMyClass(myClass);
                        student.setStudentId(studentId);


                        List<Student> students = mainJFrameService.getStudents(student);

                        List<CourseVo> list = mainJFrameService.getCourse(students) ;
                        String a[]={"年级","专业","班级","学号","姓名","性别","课程名","分数"};
                        String[][] d = ListUtils.ListToTwoArrayCourse(list,a.length);


                        defaultTableModel.setDataVector(d,a);



                        jTable.setModel(defaultTableModel);


                    }
                });



                topPanel.remove(topPanelButton3);

                topPanelButton3 = new TopPanelButton("保存", new Color(253, 184, 0), new Color(198, 151, 26));

                topPanelButton3.setBounds(1150,33,70,30);

                topPanel.add(topPanelButton3);

                topPanelButton3.addMouseListener(new MouseAdapter() {

                    public void mouseClicked(MouseEvent e) {

                        Vector<Vector> dataVector = defaultTableModel.getDataVector();




                        mainJFrameService.saveCourse(dataVector);



                    }
                });






                List<Student> students = mainJFrameService.getStudents(new Student());

                List<CourseVo> list = mainJFrameService.getCourse(students) ;
                String a[]={"年级","专业","班级","学号","姓名","性别","课程名","分数"};
                String[][] d = ListUtils.ListToTwoArrayCourse(list,a.length);


                defaultTableModel.setDataVector(d,a);



                jTable.setModel(defaultTableModel);
            }
        });
        leftPanel.add(jButton4);


        mainJFrame.add(leftPanel);
        mainJFrame.add(topPanel);
        mainJFrame.add(centerPanel);
















        mainJFrame.setVisible(true);
        return mainJFrame;


    }

    private JPanel createTopPanelAndCenterPanel()
    {

        topPanel=new JPanel();
        topPanel.setBounds(200, 0, 1400, 100);
        topPanel.setBackground(Color.WHITE);
        topPanel.setLayout(null);

        centerPanel = new JPanel();
        centerPanel.setBounds(200, 100, 1337, 740);
        centerPanel.setBackground(Color.WHITE);
        centerPanel.setLayout(new BorderLayout());

        jLabel = new JLabel("年级:");
        jLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        jComboBox = new JComboBox<>();
        jComboBox.addItem("未知");
        jComboBox.addItem("大一");
        jComboBox.addItem("大二");
        jComboBox.addItem("大三");
        jComboBox.addItem("大四");

        jLabel.setBounds(20,19,50,50);
        topPanel.add(jLabel);
        jComboBox.setBounds(80,33,80,20);
        topPanel.add(jComboBox);



        topPanelJTextField = new TopPanelJTextField(common.PROFESSION_PROMPT);
        jLabel1 = new JLabel("专业:");
        jLabel1.setFont(new Font("宋体", Font.PLAIN, 20));
        jLabel1.setBounds(220,19,50,50);
        topPanelJTextField.setBounds(280,33,120,25);

        topPanel.add(jLabel1);
        topPanel.add(topPanelJTextField);

         jLabel2 = new JLabel("班级:");
        jLabel2.setFont(new Font("宋体", Font.PLAIN, 20));
        jLabel2.setBounds(460,19,50,50);
        jComboBox1 = new JComboBox<>();
        jComboBox1.addItem("未知");
        jComboBox1.addItem("一班");
        jComboBox1.addItem("二班");
        jComboBox1.addItem("三班");
        jComboBox1.addItem("四班");


        jComboBox1.setBounds(520,33,80,20);

        topPanel.add(jLabel2);
        topPanel.add(jComboBox1);



        topPanelJTextField1 = new TopPanelJTextField(common.ID_PROMPT);
        jLabel3 = new JLabel("学号:");
        jLabel3.setFont(new Font("宋体", Font.PLAIN, 20));
        jLabel3.setBounds(640,19,50,50);
        topPanelJTextField1.setBounds(700,33,120,25);

        topPanel.add(jLabel3);
        topPanel.add(topPanelJTextField1);



         topPanelButton = new TopPanelButton("查询", Color.BLACK, Color.DARK_GRAY);

        topPanelButton.setBounds(850,33,70,30);

        topPanel.add(topPanelButton);

         topPanelButton1 = new TopPanelButton("添加", new Color(253, 184, 0), new Color(198, 151, 26));

        topPanelButton1.setBounds(950,33,70,30);




        topPanel.add(topPanelButton1);

         topPanelButton2 = new TopPanelButton("删除", new Color(253, 184, 0), new Color(198, 151, 26));

        topPanelButton2.setBounds(1050,33,70,30);

        topPanel.add(topPanelButton2);

         topPanelButton3 = new TopPanelButton("保存", new Color(253, 184, 0), new Color(198, 151, 26));

        topPanelButton3.setBounds(1150,33,70,30);

        topPanel.add(topPanelButton3);


        String a[]={"年级","专业","班级","学号","姓名","性别","年龄","出生日期","国籍","家庭地址","是否优待家庭","个人电话号码"};




        String c[]={"","","","","","","","",""};

        Student student = new Student();
        List<Student> students = mainJFrameService.getStudents(student);


        String[][] d = ListUtils.ListToTwoArrayStudent(students, a.length);


        defaultTableModel = new DefaultTableModel(d,a);
         jTable = new JTable(defaultTableModel);


        topPanelButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Student student = new Student();
                String grade,profession,myClass,studentId;

                grade=jComboBox.getSelectedItem().toString();
                profession=topPanelJTextField.getText();
                myClass=jComboBox1.getSelectedItem().toString();
                studentId=topPanelJTextField1.getText();

                student.setGrade(grade);
                student.setProfession(profession);
                student.setMyClass(myClass);
                student.setStudentId(studentId);


                List<Student> students = mainJFrameService.getStudents(student);


                String[][] d = ListUtils.ListToTwoArrayStudent(students, a.length);


                defaultTableModel.setDataVector(d,a);



                jTable.setModel(defaultTableModel);


            }
        });


        topPanelButton1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int[] selectedRows = jTable.getSelectedRows();
                if (selectedRows.length > 0) {

                    defaultTableModel.insertRow(selectedRows[selectedRows.length-1]+1,a);
                }
                else {
                    defaultTableModel.addRow(c);
                }

            }
        });

        topPanelButton2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int[] selectedRows = jTable.getSelectedRows();
                if(selectedRows.length>0) {

                    while (selectedRows.length > 0) {
                        defaultTableModel.removeRow(selectedRows[0]);
                        selectedRows = jTable.getSelectedRows();
                    }
                }else {

                    defaultTableModel.removeRow(defaultTableModel.getRowCount()-1);


                }

            }
        });

        topPanelButton3.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                Vector<Vector> dataVector = defaultTableModel.getDataVector();


                    mainJFrameService.saveStudent(dataVector);



            }
        });

        JScrollPane jScrollPane = new JScrollPane(jTable);
        centerPanel.add(jScrollPane,BorderLayout.CENTER);


        return  topPanel;
    }
}



